﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class DocCatController : Base
{
    private readonly ApplicationContext _applicationContext;
    private readonly DocCatService _docCatService;
    public DocCatController(ApplicationContext applicationContext, DocCatService docCatService)
    {
        _applicationContext = applicationContext;
        _docCatService = docCatService;
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.DocCat_Index)]
    public async Task<IActionResult> IndexAsync()
    {
        var pagedList = await _docCatService.GetCategorys();
        return Success(new { data = pagedList });
    }

    /// <summary>
    /// 输出JSON 内容管理的菜单列表使用
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.DocCat_Index)]
    public async Task<IActionResult> GetTree()
    {
        var allTree = await _docCatService.GetCatTree(await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync());
        if (allTree == null)
            return Fail("没有资源");
        else
            return Success(new { data = allTree });
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.DocCat_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        var data = await _docCatService.GetInfo(id);
        var user = await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync();
        IEnumerable<CategoryViewModel> catList = await _docCatService.GetCateListAsync(user, (int)AdminRoleEnum.Role.判断权限);
        return Success(new { data, catList });
    }

    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.DocCat_AddOrEdit)]
    [Log(Name = "新增公文管理类别")]
    public async Task<IActionResult> CreateAsync([FromBody] EditDocCat cateModel)
    {
        DocCat cate = cateModel.Adapt<DocCat>();
        await _docCatService.InsertAsync(cate);
        return Success("新增成功" + cate.Name);
    }


    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.DocCat_AddOrEdit)]
    [Log(Name = "修改公文管理类别")]
    public async Task<IActionResult> EditAsync([FromBody] EditDocCat cateModel)
    {
        DocCat articleCate = await _docCatService.GetInfo(cateModel.Id);
        articleCate.Pid = cateModel.Pid;
        articleCate.Name = cateModel.Name;
        articleCate.Ico = cateModel.Ico;
        articleCate.Sort = cateModel.Sort;
        articleCate.Status = cateModel.Status;
        articleCate.Type = cateModel.Type;
        await _docCatService.UpdateAsync(articleCate);
        return Success($"修改公文管理类别:{articleCate.Name} Id:{cateModel.Id}");
    }

    [HttpDelete, ActionName("Delete")]
    [ActionPermissionFilter(Permission = PermissionKeys.DocCat_Delete)]
    [Log(Name = "删除公文管理类别")]
    public async Task<IActionResult> DeleteAsync(int id)
    {
        if (await _docCatService.DeleteDelCache(id))
            return Success("删除公文管理类别 Id: " + id);
        else
            return Fail("删除失败！可能因为当前类别下有内容！");
    }

}
